import select

class Proxy:
	def __init__(self, subject, callback):
		self.subject = subject
		self.callback = callback
	
	def fileno(self):
		return self.subject.fileno()
	
	def on_io(self, *args, **kwargs):
		return self.callback(*args, **kwargs)

class IO:
	def __init__(self):
		self.poll = select.poll()
		self.listeners = {}
	
	def register(self, subject, event):
		self.listeners[subject.fileno()] = subject
		self.poll.register(subject.fileno(), event)
	
	def unregister(self, subject):
		del self.listeners[subject.fileno()]
		self.poll.unregister(subject.fileno())
	
	def wait(self):
		for fd, state in self.poll.poll():
			self.listeners[fd].on_io(state)
	
	def run(self):
		while True:
			self.wait()
	

for i in dir(select):
	if i.startswith("POLL") or i.startswith("EPOLL"):
		globals()[i] = getattr(select, i)

default_io = IO()

def get_default_io():
	return default_io

